<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../dbcom/MerchantCom.php");

$merchantcom = new MerchantCom();

$cont = $_REQUEST['cont'];
switch($cont){
    case 1://新增
        $name = $_REQUEST['name'];
        $mobile = $_REQUEST['mobile'];
        $address = $_REQUEST['address'];
        $code = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        $res = $merchantcom->addMerchant($code, $name, $mobile, $address);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "add userinfo fail"));
        }
        break;
    case 2://查
        $id = $_REQUEST['id'];
        $merchant = $merchantcom->getMerchant($id);
        $resdata = array();
        if($merchant){
            $resdata['name'] = $merchant['name'];
            $resdata['mobile'] = $merchant['mobile'];
            $resdata['address'] = $merchant['address'];
            echo json_encode(array("code" => 0, "msg" => "success", "data" => $resdata));
        }else{
            echo json_encode(array("code" => 400, "msg" => "get merchant fail"));
        }
        break;
    case 3://修改
        $id = $_REQUEST['id'];
        $name = $_REQUEST['name'];
        $address = $_REQUEST['address'];
        $mobile = $_REQUEST['mobile'];
        $res = $merchantcom->saveMerchant($id, $name, $mobile, $address);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "save merchant fail"));
        }
        break;
    case 4://删除
        $id = $_REQUEST['id'];
        $res = $merchantcom->deleteMerchant($id);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "delete merchant fail"));
        }
        break;
    case 5://批量删除
        $ids = $_REQUEST['ids'];
        $idarr = explode(",", $ids);
        foreach($idarr as $v){
            $res = $merchantcom->deleteMerchant($v);
            if(!$res){
                echo json_encode(array("code" => 400, "msg" => "delete merchant fail"));
                exit();
            }
        }
        echo json_encode(array("code" => 0, "msg" => "success"));
        break;
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined fail"));
        break;
}
exit();
